<style>
</style>
<template>
  <list_url ref="list_url" :config="config">
    <template #action="{row}">
      <el-button @click="to_edit(row)" size="small">详情</el-button>
    </template>
  </list_url>
</template>
<script>
import s94Mjs from "s94-js";
import {iframe, in_right_admin} from "@/js/tool.js";
import {_config, decode_config, s94_event} from "@/js/s94_tool.js";
import {ajax} from "@/js/api_tool.js";

export default {
  props: ['__iframe'],
  data(){
    let app = this, btns = {}, is_del = this.$get('is_del');
    let model_name = this.$get('model_name','');
    if (is_del){
      if(in_right_admin('Category/restore')) btns['恢复'] = function(){ app.$refs['list_url'].restore() }
      if(in_right_admin('Category/delete')) btns['彻底删除'] = function(){ app.$refs['list_url'].delete() }
    }else {
      if(in_right_admin('Category/add')) btns['添加'] = function(){ iframe('category/edit?model_name='+model_name) }
      if(in_right_admin('Category/del')) btns['删除'] = function(){ app.$refs['list_url'].del() }
      if(in_right_admin('Category/recycle')) btns['回收站'] = function(){ iframe('category/index?is_del=1') }
    }
    let model_name_config = decode_config(_config('model_map'), {content_key:'label', value_key:'name'});
    let forms_config = {
      model_name: {label:'模型', name:'where[model_name][eq]', type:model_name?'hidden':'select_one', config:model_name_config, value: model_name},
      alias: {label:'类目名', name:'where[name][like]', type:'text_one',value:''}
    };
    let list_cols = {
      id: {name:'id', label:'ID', width:'65px', fixed:'left', type: ''},
      model_name: {name:'model_name', label:'模型', type:'select_one', config:model_name_config, width:'100px'},
      head_img: {name:'head_img', label:'缩略图', width:'80px', type:'file_img'},
      alias: {name:'alias', label:'类目名', align:'left', min_width:'200px'},
      info: {name:'info', label:'描述', min_width:'100px'},
      data_num: {name:'data_num', label:'内容数量', width:'100px'},
      is_show: {name:'is_show', label:'是否显示', type:'radio_switch', can_edit:true, config:['否','是'], width: '80px', onchange:this.edit},
      sort: {name:'sort', label:'排序', type:'text_one', config:'number', can_edit:true, width:'80px', onchange:this.edit},
    };
    if (is_del){
      list_cols['delete_time'] = {name:'delete_time', label:'删除时间', width:'165px', type:'datetime'};
    }else {
      list_cols['create_time'] = {name:'create_time', label:'创建时间', width:'165px'};
    }
    list_cols['action'] = {name:'action', label:'操作', width:'80px', type:'action', fixed:'right'};

    return {
      config: {
        api: '/admin/category/'+(is_del?'recycle':'list'),
        title: "类目列表"+(is_del?'(已删除)':''),
        btns: btns,
        forms_config: forms_config,
        list_cols: list_cols,
        resdata: function(list){
          let ids=[], parent_ids=[], res=[];
          s94Mjs.each(list, function(v){
            ids.push(v.id);
          })
          s94Mjs.each(list, function(v){
            if (!~ids.indexOf(v.parent_id) && !~parent_ids.indexOf(v.parent_id)){
              parent_ids.push(v.parent_id);
            }
          })
          function loop(list, parent_id, prefix){
            var res=[];
            s94Mjs.each(list, function(row) {
              if (row['parent_id'] != parent_id) return;
              row['alias'] = prefix+row['title'];
              res.push(row);
              var last_prefix = "&nbsp;&nbsp;&nbsp;&nbsp;"+(prefix?prefix:"└ ");
              res = res.concat(loop(list, row['id'], last_prefix));
            });
            return res;
          }
          parent_ids.forEach(function(v){
            res = res.concat(loop(list, v, ''));
          })
          return res;
        },
      },
    }
  },
  methods: {
    to_edit(row){
      let can_edit = in_right_admin('Category/edit') && ~[0,row.tenant_id].indexOf(_config('this_tenant.id'))
      let param = {
        id: row.id,
      }
      if (!can_edit) param['is_info'] = 1;
      iframe('category/edit?'+s94Mjs.param_encode(param))
    },
    edit(row,col,v){
      let app = this;
      let can_edit = in_right_admin('Category/edit') && !!~[0,row.tenant_id].indexOf(_config('this_tenant.id'))
      if(!can_edit) return;
      ajax(`/admin/category/edit?id=${row.id}&${col.name}=${v}`, function(res){
        if (col.type=='radio_switch' && res.data==0) app.$refs['list_url'].list();
        if (col.type!='radio_switch' && res.data>0) app.$refs['list_url'].list();
      })
    },
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;

    s94_event(['category/add','category/edit'], (evt)=>{
      console.log(evt, 'category/index')
      this.$refs.list_url.list();
    })
  },
  unmounted() {
    s94_event(['category/add','category/edit'], false);
  }
}
</script>
